home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_110 / mint / bash110s.zoo / bash-1.10 / parse.y < prev    next >
Encoding:
Text File  |  1991-10-03  |  64.9 KB  |  2,565 lines

  1. /* Yacc grammar for bash. */
  2.  
  3. /* Copyright (C) 1989 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7.    Bash is free software; you can redistribute it and/or modify it under
  8.    the terms of the GNU General Public License as published by the Free
  9.    Software Foundation; either version 1, or (at your option) any later
  10.    version.
  11.  
  12.    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13.    WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14.    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15.    for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License along
  18.    with Bash; see the file LICENSE.  If not, write to the Free Software
  19.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. %{
  22. #include <stdio.h>
  23. #include <signal.h>
  24. #include "shell.h"
  25. #include "flags.h"
  26.  
  27. #if defined (READLINE)
  28. #include <readline/readline.h>
  29. #endif /* READLINE */
  30.  
  31. #include <readline/history.h>
  32.  
  33. #if defined (JOB_CONTROL)
  34. #  include <sys/types.h>
  35. #  include "jobs.h"
  36. #endif /* JOB_CONTROL */
  37.  
  38. #define YYDEBUG 1
  39. extern int eof_encountered;
  40. extern int no_line_editing;
  41. extern int interactive, interactive_shell;
  42.  
  43. /* **************************************************************** */
  44. /*                                    */
  45. /*            "Forward" declarations                */
  46. /*                                    */
  47. /* **************************************************************** */
  48.  
  49. /* This is kind of sickening.  In order to let these variables be seen by
  50.    all the functions that need them, I am forced to place their declarations
  51.    far away from the place where they should logically be found. */
  52.  
  53. static int reserved_word_acceptable ();
  54.  
  55. /* PROMPT_STRING_POINTER points to one of these, never to an actual string. */
  56. char *ps1_prompt, *ps2_prompt;
  57.  
  58. /* Handle on the current prompt string.  Indirectly points through
  59.    ps1_ or ps2_prompt. */
  60. char **prompt_string_pointer = (char **)NULL;
  61. char *current_prompt_string;
  62.  
  63. /* Variables to manage the task of reading here documents, because we need to
  64.    defer the reading until after a complete command has been collected. */
  65. REDIRECT *redirection_needing_here_doc = (REDIRECT *)NULL;
  66. int need_here_doc = 0;
  67. %}
  68.  
  69. %union {
  70.   WORD_DESC *word;        /* the word that we read. */
  71.   int number;            /* the number that we read. */
  72.   WORD_LIST *word_list;
  73.   COMMAND *command;
  74.   REDIRECT *redirect;
  75.   ELEMENT element;
  76.   PATTERN_LIST *pattern;
  77. }
  78.  
  79. /* Reserved words.  Members of the first group are only recognized
  80.    in the case that they are preceded by a list_terminator.  Members
  81.    of the second group are recognized only under special circumstances. */
  82. %token IF THEN ELSE ELIF FI CASE ESAC FOR WHILE UNTIL DO DONE FUNCTION
  83. %token IN BANG
  84.  
  85. /* More general tokens. yylex () knows how to make these. */
  86. %token <word> WORD
  87. %token <number> NUMBER
  88. %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND
  89. %token GREATER_AND SEMI_SEMI LESS_LESS_MINUS AND_GREATER LESS_GREATER
  90. %token GREATER_BAR
  91.  
  92. /* The types that the various syntactical units return. */
  93.  
  94. %type <command> inputunit command list list0 list1 simple_list simple_list1 simple_command shell_command group_command
  95. %type <command> elif_clause pipeline
  96. %type <redirect> redirection redirections
  97. %type <element> simple_command_element
  98. %type <word_list> words pattern 
  99. %type <pattern> pattern_list case_clause_sequence case_clause_1 pattern_list_1
  100.  
  101. %start inputunit
  102.  
  103. %left '&' ';' '\n' yacc_EOF
  104. %left AND_AND OR_OR
  105. %right '|'
  106. %%
  107.  
  108. inputunit:    simple_list '\n'
  109.             {
  110.               /* Case of regular command.  Discard the error
  111.                  safety net,and return the command just parsed. */
  112.               global_command = $1;
  113.               eof_encountered = 0;
  114.               discard_parser_constructs (0);
  115.               YYACCEPT;
  116.             }
  117.     |    '\n'
  118.             {
  119.               /* Case of regular command, but not a very
  120.                  interesting one.  Return a NULL command. */
  121.               global_command = (COMMAND *)NULL;
  122.               YYACCEPT;
  123.             }
  124.     |
  125.         error '\n'
  126.             {
  127.               /* Error during parsing.  Return NULL command. */
  128.               global_command = (COMMAND *)NULL;
  129.               eof_encountered = 0;
  130.               discard_parser_constructs (1);
  131.               if (interactive)
  132.                 {
  133.                   YYACCEPT;
  134.                 }
  135.               else
  136.                 {
  137.                   YYABORT;
  138.                 }
  139.             }
  140.     |    yacc_EOF
  141.             {
  142.               /* Case of EOF seen by itself.  Do ignoreeof or 
  143.                  not. */
  144.               global_command = (COMMAND *)NULL;
  145.               handle_eof_input_unit ();
  146.               YYACCEPT;
  147.             }
  148.     ;
  149.  
  150. words:    
  151.             { $$ = (WORD_LIST *)NULL; }
  152.     |    words WORD
  153.             { $$ = make_word_list ($2, $1); }
  154.     ;
  155.  
  156. redirection:    '>' WORD
  157.             { $$ = make_redirection ( 1, r_output_direction, $2); }
  158.     |    '<' WORD
  159.             { $$ = make_redirection ( 0, r_input_direction, $2); }
  160.     |    NUMBER '>' WORD
  161.             { $$ = make_redirection ($1, r_output_direction, $3); }
  162.     |    NUMBER '<' WORD
  163.             { $$ = make_redirection ($1, r_input_direction, $3); }
  164.     |    GREATER_GREATER WORD
  165.             { $$ = make_redirection ( 1, r_appending_to, $2); }
  166.     |    NUMBER GREATER_GREATER WORD
  167.             { $$ = make_redirection ($1, r_appending_to, $3); }
  168.     |    LESS_LESS WORD
  169.             {
  170.               $$ = make_redirection ( 0, r_reading_until, $2);
  171.               redirection_needing_here_doc = $$;
  172.               need_here_doc = 1;
  173.             }
  174.     |    NUMBER LESS_LESS WORD
  175.             {
  176.               $$ = make_redirection ($1, r_reading_until, $3);
  177.               redirection_needing_here_doc = $$;
  178.               need_here_doc = 1;
  179.             }
  180.     |    LESS_AND NUMBER
  181.             { $$ = make_redirection ( 0, r_duplicating, $2); }
  182.     |    NUMBER LESS_AND NUMBER
  183.             { $$ = make_redirection ($1, r_duplicating, $3); }
  184.     |    GREATER_AND NUMBER
  185.             { $$ = make_redirection ( 1, r_duplicating, $2); }
  186.     |    NUMBER GREATER_AND NUMBER
  187.             { $$ = make_redirection ($1, r_duplicating, $3); }
  188.     |    LESS_LESS_MINUS WORD
  189.             {
  190.               $$ = make_redirection ( 0, r_deblank_reading_until, $2);
  191.               redirection_needing_here_doc = $$;
  192.               need_here_doc = 1;
  193.             }
  194.     |    NUMBER LESS_LESS_MINUS WORD
  195.             {
  196.               $$ = make_redirection ($1, r_deblank_reading_until, $3);
  197.               redirection_needing_here_doc = $$;
  198.               need_here_doc = 1;
  199.             }
  200.     |    GREATER_AND '-'
  201.             { $$ = make_redirection ( 1, r_close_this, 0); }
  202.     |    NUMBER GREATER_AND '-'
  203.             { $$ = make_redirection ($1, r_close_this, 0); }
  204.     |    LESS_AND '-'
  205.             { $$ = make_redirection ( 0, r_close_this, 0); }
  206.     |    NUMBER LESS_AND '-'
  207.             { $$ = make_redirection ($1, r_close_this, 0); }
  208.     |    AND_GREATER WORD
  209.             { $$ = make_redirection ( 1, r_err_and_out, $2); }
  210.     |    GREATER_AND WORD
  211.             { $$ = make_redirection ( 1, r_err_and_out, $2); }
  212.     |    NUMBER LESS_GREATER WORD
  213.             { $$ = make_redirection ( $1, r_input_output, $3); }
  214.     |    LESS_GREATER WORD
  215.             {
  216.               REDIRECT *t1, *t2;
  217.               extern WORD_DESC *copy_word ();
  218.  
  219.               t1 = make_redirection ( 0, r_input_direction, $2);
  220.               t2 = make_redirection ( 1, r_output_direction, copy_word ($2));
  221.               t1->next = t2;
  222.               $$ = t1;
  223.             }              
  224.     |    GREATER_BAR WORD
  225.             { $$ = make_redirection ( 1, r_output_force, $2); }
  226.     |    NUMBER GREATER_BAR WORD
  227.             { $$ = make_redirection ( $1, r_output_force, $3); }
  228.     ;
  229.  
  230. simple_command_element: WORD
  231.             { $$.word = $1; $$.redirect = 0; }
  232.     |    redirection
  233.             { $$.redirect = $1; $$.word = 0; }
  234.     ;
  235.  
  236. redirections:    redirection
  237.             {
  238.               $$ = $1;
  239.             }
  240.     |    redirections redirection
  241.             { 
  242.               register REDIRECT *t = $1;
  243.  
  244.               while (t->next)
  245.                 t = t->next;
  246.               t->next = $2; 
  247.               $$ = $1;
  248.             }
  249.     ;
  250.  
  251. simple_command:    simple_command_element
  252.             { $$ = make_simple_command ($1, (COMMAND *)NULL); }
  253.     |    simple_command simple_command_element
  254.             { $$ = make_simple_command ($2, $1); }
  255.     ;
  256.  
  257. command:    simple_command
  258.             { $$ = clean_simple_command ($1); }
  259.  
  260.     |    shell_command
  261.             { $$ = $1; }
  262.  
  263.     |    shell_command redirections
  264.             {
  265.               $$->redirects = $2;
  266.               $$ = $1;
  267.             }
  268.     ;
  269.  
  270. shell_command:    FOR WORD newlines DO list DONE
  271.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $5); }
  272.     |    FOR WORD newlines '{' list '}'
  273.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("$@", (WORD_LIST *)NULL), $5); }
  274.     |    FOR WORD ';' newlines DO list DONE
  275.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
  276.     |    FOR WORD ';' newlines '{' list '}'
  277.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("\"$@\"", (WORD_LIST *)NULL), $6); }
  278.  
  279.     |    FOR WORD newlines IN words list_terminator newlines DO list DONE
  280.             { $$ = make_for_command ($2, (WORD_LIST *)reverse_list ($5), $9); }
  281.     |    FOR WORD newlines IN words list_terminator newlines '{' list '}'
  282.             { $$ = make_for_command ($2, (WORD_LIST *)reverse_list ($5), $9); }
  283.  
  284.     |    CASE WORD newlines IN newlines ESAC
  285.             { $$ = make_case_command ($2, (PATTERN_LIST *)NULL); }
  286.     |    CASE WORD newlines IN case_clause_sequence newlines ESAC
  287.             { $$ = make_case_command ($2, $5); }
  288.     |    CASE WORD newlines IN case_clause_1 ESAC
  289.             { /* Nobody likes this...
  290.                  report_syntax_error ("Inserted `;;'"); */
  291.               $$ = make_case_command ($2, $5); }
  292.  
  293.     |    IF list THEN list FI
  294.             { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
  295.     |    IF list THEN list ELSE list FI
  296.             { $$ = make_if_command ($2, $4, $6); }
  297.     |    IF list THEN list elif_clause FI
  298.             { $$ = make_if_command ($2, $4, $5); }
  299.  
  300.     |    WHILE list DO list DONE
  301.             { $$ = make_while_command ($2, $4); }
  302.     |    UNTIL list DO list DONE
  303.             { $$ = make_until_command ($2, $4); }
  304.  
  305.     |    '(' list ')'
  306.             { $2->flags |= CMD_WANT_SUBSHELL; $$ = $2; }
  307.  
  308.     |    group_command
  309.             { $$ = $1; }
  310.  
  311.     |    WORD '(' ')' newlines group_command
  312.             { $$ = make_function_def ($1, $5); }
  313.  
  314.     |    FUNCTION WORD '(' ')' newlines group_command
  315.             { $$ = make_function_def ($2, $6); }
  316.  
  317.     |    FUNCTION WORD newlines group_command
  318.             { $$ = make_function_def ($2, $4); }
  319.     ;
  320.  
  321. group_command:    '{' list '}'
  322.             { $$ = make_group_command ($2); }
  323.     ;
  324.  
  325. elif_clause:    ELIF list THEN list
  326.             { $$ = make_if_command ($2, $4, (COMMAND *)NULL); }
  327.     |    ELIF list THEN list ELSE list
  328.             { $$ = make_if_command ($2, $4, $6); }
  329.     |    ELIF list THEN list elif_clause
  330.             { $$ = make_if_command ($2, $4, $5); }
  331.     ;
  332.  
  333.  
  334. case_clause_1:    pattern_list_1
  335.     |    case_clause_sequence pattern_list_1
  336.             { $2->next = $1; $$ = $2; }
  337.     ;
  338.  
  339. pattern_list_1:    newlines pattern ')' list
  340.             { $$ = make_pattern_list ($2, $4); }
  341.     |    newlines pattern ')' newlines
  342.             { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
  343.     ;
  344.  
  345. case_clause_sequence:  pattern_list
  346.  
  347.     |    case_clause_sequence pattern_list
  348.             { $2->next = $1; $$ = $2; }
  349.     ;
  350.  
  351. pattern_list:    newlines pattern ')' list SEMI_SEMI
  352.             { $$ = make_pattern_list ($2, $4); }
  353.     |    newlines pattern ')' newlines SEMI_SEMI
  354.             { $$ = make_pattern_list ($2, (COMMAND *)NULL); }
  355.     ;
  356.  
  357. pattern:    WORD
  358.             { $$ = make_word_list ($1, (WORD_LIST *)NULL); }
  359.     |    pattern '|' WORD
  360.             { $$ = make_word_list ($3, $1); }
  361.     ;
  362.  
  363. /* A list allows leading or trailing newlines and
  364.    newlines as operators (equivalent to semicolons).
  365.    It must end with a newline or semicolon.
  366.    Lists are used within commands such as if, for, while.  */
  367.  
  368. list:        newlines list0
  369.             {
  370.               $$ = $2;
  371.               if (need_here_doc)
  372.                 make_here_document (redirection_needing_here_doc);
  373.               need_here_doc = 0;
  374.              }
  375.     ;
  376.  
  377. list0:        list1
  378.     |    list1 '\n' newlines
  379.     |    list1 '&' newlines
  380.             { $$ = command_connect ($1, 0, '&'); }
  381.     |    list1 ';' newlines
  382.  
  383.     ;
  384.  
  385. list1:        list1 AND_AND newlines list1
  386.             { $$ = command_connect ($1, $4, AND_AND); }
  387.     |    list1 OR_OR newlines list1
  388.             { $$ = command_connect ($1, $4, OR_OR); }
  389.     |    list1 '&' newlines list1
  390.             { $$ = command_connect ($1, $4, '&'); }
  391.     |    list1 ';' newlines list1
  392.             { $$ = command_connect ($1, $4, ';'); }
  393.     |    list1 '\n' newlines list1
  394.             { $$ = command_connect ($1, $4, ';'); }
  395.     |    pipeline
  396.             { $$ = $1; }
  397.     |    BANG pipeline
  398.             {
  399.               $2->flags |= CMD_INVERT_RETURN;
  400.               $$ = $2;
  401.             }
  402.     ;
  403.  
  404. list_terminator:'\n'
  405.     |    ';'
  406.     |    yacc_EOF
  407.     ;
  408.  
  409. newlines:
  410.     |    newlines '\n'
  411.     ;
  412.  
  413. /* A simple_list is a list that contains no significant newlines
  414.    and no leading or trailing newlines.  Newlines are allowed
  415.    only following operators, where they are not significant.
  416.  
  417.    This is what an inputunit consists of.  */
  418.  
  419. simple_list:    simple_list1
  420.             {
  421.               $$ = $1;
  422.               if (need_here_doc)
  423.                 make_here_document (redirection_needing_here_doc);
  424.               need_here_doc = 0;
  425.             }
  426.     |    simple_list1 '&'
  427.             {
  428.               $$ = command_connect ($1, (COMMAND *)NULL, '&');
  429.               if (need_here_doc)
  430.                 make_here_document (redirection_needing_here_doc);
  431.               need_here_doc = 0;
  432.             }
  433.     |    simple_list1 ';'
  434.             {
  435.               $$ = $1;
  436.               if (need_here_doc)
  437.                 make_here_document (redirection_needing_here_doc);
  438.               need_here_doc = 0;
  439.             }
  440.     ;
  441.  
  442. simple_list1:    simple_list1 AND_AND newlines simple_list1
  443.             { $$ = command_connect ($1, $4, AND_AND); }
  444.     |    simple_list1 OR_OR newlines simple_list1
  445.             { $$ = command_connect ($1, $4, OR_OR); }
  446.     |    simple_list1 '&' simple_list1
  447.             { $$ = command_connect ($1, $3, '&'); }
  448.     |    simple_list1 ';' simple_list1
  449.             { $$ = command_connect ($1, $3, ';'); }
  450.     |    pipeline
  451.             { $$ = $1; }
  452.     |    BANG pipeline
  453.             {
  454.               $2->flags |= CMD_INVERT_RETURN;
  455.               $$ = $2;
  456.             }
  457.     ;
  458.  
  459. pipeline:
  460.         pipeline '|' newlines pipeline
  461.             { $$ = command_connect ($1, $4, '|'); }
  462.     |    command
  463.             { $$ = $1; }
  464.     ;
  465. %%
  466.  
  467. /* Initial size to allocate for tokens, and the
  468.    amount to grow them by. */
  469. #define TOKEN_DEFAULT_GROW_SIZE 512
  470.  
  471. /* The token currently being read. */
  472. int current_token = 0;
  473.  
  474. /* The last read token, or NULL.  read_token () uses this for context
  475.    checking. */
  476. int last_read_token = 0;
  477.  
  478. /* The token read prior to last_read_token. */
  479. int token_before_that = 0;
  480.  
  481. /* Global var is non-zero when end of file has been reached. */
  482. int EOF_Reached = 0;
  483.  
  484. /* yy_getc () returns the next available character from input or EOF.
  485.    yy_ungetc (c) makes `c' the next character to read.
  486.    init_yy_io (get, unget), makes the function `get' the installed function
  487.    for getting the next character, and makes `unget' the installed function
  488.    for un-getting a character. */
  489. return_EOF ()            /* does nothing good. */
  490. {
  491.   return (EOF);
  492. }
  493.  
  494. /* Variables containing the current get and unget functions. */
  495.  
  496. /* Some stream `types'. */
  497. #define st_stream 0
  498. #define st_string 1
  499.  
  500. Function *get_yy_char = return_EOF;
  501. Function *unget_yy_char = return_EOF;
  502. int yy_input_type = st_stream;
  503. FILE *yy_input_dev = (FILE *)NULL;
  504.  
  505. /* The current stream name.  In the case of a file, this is a filename. */
  506. char *stream_name = (char *)NULL;
  507.  
  508. /* Function to set get_yy_char and unget_yy_char. */
  509. init_yy_io (get_function, unget_function, type, location)
  510.      Function *get_function, *unget_function;
  511.      int type;
  512.      FILE *location;
  513. {
  514.   get_yy_char = get_function;
  515.   unget_yy_char = unget_function;
  516.   yy_input_type = type;
  517.   yy_input_dev = location;
  518. }
  519.  
  520. /* Call this to get the next character of input. */
  521. yy_getc ()
  522. {
  523.   return (*get_yy_char) ();
  524. }
  525.  
  526. /* Call this to unget C.  That is, to make C the next character
  527.    to be read. */
  528. yy_ungetc (c)
  529. {
  530.   return (*unget_yy_char) (c);
  531. }
  532.  
  533. /* **************************************************************** */
  534. /*                                    */
  535. /*          Let input be read from readline ().            */
  536. /*                                    */
  537. /* **************************************************************** */
  538.  
  539. #if defined (READLINE)
  540. char *current_readline_prompt = (char *)NULL;
  541. char *current_readline_line = (char *)NULL;
  542. int current_readline_line_index = 0;
  543.  
  544. static int readline_initialized_yet = 0;
  545. int
  546. yy_readline_get ()
  547. {
  548.   if (!current_readline_line)
  549.     {
  550.       extern sighandler sigint_sighandler ();
  551.       extern int interrupt_immediately;
  552.       extern char *readline ();
  553.       SigHandler *old_sigint;
  554. #if defined (JOB_CONTROL)
  555.       extern pid_t shell_pgrp;
  556.       extern int job_control;
  557. #endif /* JOB_CONTROL */
  558.  
  559.       if (!readline_initialized_yet)
  560.     {
  561.       initialize_readline ();
  562.       readline_initialized_yet = 1;
  563.     }
  564.  
  565. #if defined (JOB_CONTROL)
  566.       if (job_control)
  567.     give_terminal_to (shell_pgrp);
  568. #endif /* JOB_CONTROL */
  569.  
  570.       old_sigint = (SigHandler *)signal (SIGINT, sigint_sighandler);
  571.       interrupt_immediately++;
  572.  
  573.       if (!current_readline_prompt)
  574.     current_readline_line = readline ("");
  575.       else
  576.     current_readline_line = readline (current_readline_prompt);
  577.  
  578.       interrupt_immediately--;
  579.       signal (SIGINT, old_sigint);
  580.  
  581.       /* Reset the prompt to whatever is in the decoded value of
  582.      prompt_string_pointer. */
  583.       reset_readline_prompt ();
  584.  
  585.       current_readline_line_index = 0;
  586.  
  587.       if (!current_readline_line)
  588.     {
  589.       current_readline_line_index = 0;
  590.       return (EOF);
  591.     }
  592.  
  593.       current_readline_line =
  594.     (char *)xrealloc (current_readline_line,
  595.               2 + strlen (current_readline_line));
  596.       strcat (current_readline_line, "\n");
  597.     }
  598.  
  599.   if (!current_readline_line[current_readline_line_index])
  600.     {
  601.       free (current_readline_line);
  602.       current_readline_line = (char *)NULL;
  603.       return (yy_readline_get ());
  604.     }
  605.   else
  606.     {
  607.       int c = current_readline_line[current_readline_line_index++];
  608.       return (c);
  609.     }
  610. }
  611.  
  612. int
  613. yy_readline_unget (c)
  614. {
  615.   if (current_readline_line_index && current_readline_line)
  616.     current_readline_line[--current_readline_line_index] = c;
  617.   return (c);
  618. }
  619.   
  620. with_input_from_stdin ()
  621. {
  622.   init_yy_io (yy_readline_get, yy_readline_unget,
  623.           st_string, (FILE *)current_readline_line);
  624.   stream_name = savestring ("readline stdin");
  625. }
  626.  
  627. #else  /* !READLINE */
  628.  
  629. with_input_from_stdin ()
  630. {
  631.   with_input_from_stream (stdin, "stdin");
  632. }
  633. #endif    /* !READLINE */
  634.  
  635. /* **************************************************************** */
  636. /*                                    */
  637. /*   Let input come from STRING.  STRING is zero terminated.        */
  638. /*                                    */
  639. /* **************************************************************** */
  640.  
  641. int
  642. yy_string_get ()
  643. {
  644.   /* If the string doesn't exist, or is empty, EOF found. */
  645.   if (!(char *)yy_input_dev || !*(char *)yy_input_dev)
  646.     return (EOF);
  647.   else
  648.     {
  649.       register char *temp = (char *)yy_input_dev;
  650.       int c = *temp++;
  651.       yy_input_dev = (FILE *)temp;
  652.       return (c);
  653.     }
  654. }
  655.  
  656. int
  657. yy_string_unget (c)
  658.      int c;
  659. {
  660.   register char *temp = (char *)yy_input_dev;
  661.   *(--temp) = c;
  662.   yy_input_dev = (FILE *)temp;
  663.   return (c);
  664. }
  665.  
  666. with_input_from_string (string, name)
  667.      char *string;
  668.      char *name;
  669. {
  670.   init_yy_io (yy_string_get, yy_string_unget, st_string, (FILE *)string);
  671.   stream_name = savestring (name);
  672. }
  673.  
  674. /* **************************************************************** */
  675. /*                                    */
  676. /*             Let input come from STREAM.            */
  677. /*                                    */
  678. /* **************************************************************** */
  679.  
  680. int
  681. yy_stream_get ()
  682. {
  683.   if (yy_input_dev)
  684. #if defined (USG)
  685.     return (sysv_getc (yy_input_dev));
  686. #else
  687.     return (getc (yy_input_dev));
  688. #endif    /* USG */
  689.   else return (EOF);
  690. }
  691.  
  692. int
  693. yy_stream_unget (c)
  694.      int c;
  695. {
  696.   return (ungetc (c, yy_input_dev));
  697. }
  698.  
  699. with_input_from_stream (stream, name)
  700.      FILE *stream;
  701.      char *name;
  702. {
  703.   init_yy_io (yy_stream_get, yy_stream_unget, st_stream, stream);
  704.   stream_name = savestring (name);
  705. }
  706.  
  707. typedef struct stream_saver {
  708.   struct stream_saver *next;
  709.   Function *getter, *putter;
  710.   int type, line;
  711.   char *location, *name;
  712. } STREAM_SAVER;
  713.  
  714. /* The globally known line number. */
  715. int line_number = 0;
  716.  
  717. STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
  718.  
  719. push_stream ()
  720. {
  721.   STREAM_SAVER *temp = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
  722.   temp->type = yy_input_type;
  723.   temp->location = (char *)yy_input_dev;
  724.   temp->getter = get_yy_char;
  725.   temp->putter = unget_yy_char;
  726.   temp->line = line_number;
  727.   temp->name = stream_name; stream_name = (char *)NULL;
  728.   temp->next = stream_list;
  729.   stream_list = temp;
  730.   EOF_Reached = line_number = 0;
  731. }
  732.  
  733. pop_stream ()
  734. {
  735.   if (!stream_list)
  736.     {
  737.       EOF_Reached = 1;
  738.     }
  739.   else
  740.     {
  741.       STREAM_SAVER *temp = stream_list;
  742.     
  743.       EOF_Reached = 0;
  744.       stream_list = stream_list->next;
  745.  
  746.       if (stream_name)
  747.     free (stream_name);
  748.       stream_name = temp->name;
  749.  
  750.       init_yy_io (temp->getter, temp->putter, temp->type, (FILE *)temp->location);
  751.       line_number = temp->line;
  752.       free (temp);
  753.     }
  754. }
  755.  
  756. /*
  757.  * This is used to inhibit alias expansion and reserved word recognition
  758.  * inside case statement pattern lists.  A `case statement pattern list'
  759.  * is:
  760.  *    everything between the `in' in a `case word in' and the next ')'
  761.  *    or `esac'
  762.  *    everything between a `;;' and the next `)' or `esac'
  763.  */
  764. static int in_case_pattern_list = 0;
  765.  
  766. #if defined (ALIAS)
  767. /*
  768.  * Pseudo-global variables used in implementing token-wise alias expansion.
  769.  */
  770.  
  771. static int expand_next_token = 0;
  772. static char *current_token_being_expanded = (char *)NULL;
  773. static char *pending_token_being_expanded = (char *)NULL;
  774.  
  775. /*
  776.  * Pushing and popping strings.  This works together with shell_getc to 
  777.  * implement alias expansion on a per-token basis.
  778.  */
  779.  
  780. typedef struct string_saver {
  781.   struct string_saver *next;
  782.   int expand_alias;  /* Value to set expand_alias to when string is popped. */
  783.   char *saved_line;
  784.   int saved_line_size, saved_line_index, saved_line_terminator;
  785.   char *saved_token_being_expanded;
  786. } STRING_SAVER;
  787.  
  788. STRING_SAVER *pushed_string_list = (STRING_SAVER *)NULL;
  789.  
  790. static void save_expansion ();
  791.  
  792. /*
  793.  * Push the current shell_input_line onto a stack of such lines and make S
  794.  * the current input.  Used when expanding aliases.  EXPAND is used to set
  795.  * the value of expand_next_token when the string is popped, so that the
  796.  * word after the alias in the original line is handled correctly when the
  797.  * alias expands to multiple words.  TOKEN is the token that was expanded
  798.  * into S; it is saved and used to prevent infinite recursive expansion.
  799.  */
  800. static void
  801. push_string (s, expand, token)
  802.      char *s;
  803.      int expand;
  804.      char *token;
  805. {
  806.   extern char *shell_input_line;
  807.   extern int shell_input_line_size, shell_input_line_index,
  808.          shell_input_line_terminator;
  809.   STRING_SAVER *temp = (STRING_SAVER *) xmalloc (sizeof (STRING_SAVER));
  810.  
  811.   temp->expand_alias = expand;
  812.   temp->saved_line = shell_input_line;
  813.   temp->saved_line_size = shell_input_line_size;
  814.   temp->saved_line_index = shell_input_line_index;
  815.   temp->saved_line_terminator = shell_input_line_terminator;
  816.   temp->saved_token_being_expanded = current_token_being_expanded;
  817.   temp->next = pushed_string_list;
  818.   pushed_string_list = temp;
  819.  
  820.   save_expansion (token);
  821.  
  822.   current_token_being_expanded = token;
  823.   shell_input_line = s;
  824.   shell_input_line_size = strlen (s);
  825.   shell_input_line_index = 0;
  826.   shell_input_line_terminator = '\0';
  827.   expand_next_token = 0;
  828. }
  829.  
  830. /*
  831.  * Make the top of the pushed_string stack be the current shell input.
  832.  * Only called when there is something on the stack.  Called from shell_getc
  833.  * when it thinks it has consumed the string generated by an alias expansion
  834.  * and needs to return to the original input line.
  835.  */
  836. static void
  837. pop_string ()
  838. {
  839.   extern char *shell_input_line;
  840.   extern int shell_input_line_size, shell_input_line_index,
  841.          shell_input_line_terminator;
  842.   STRING_SAVER *t;
  843.  
  844.   if (shell_input_line)
  845.     free (shell_input_line);
  846.   shell_input_line = pushed_string_list->saved_line;
  847.   shell_input_line_index = pushed_string_list->saved_line_index;
  848.   shell_input_line_size = pushed_string_list->saved_line_size;
  849.   shell_input_line_terminator = pushed_string_list->saved_line_terminator;
  850.   expand_next_token = pushed_string_list->expand_alias;
  851.   pending_token_being_expanded = pushed_string_list->saved_token_being_expanded;
  852.   t = pushed_string_list;
  853.   pushed_string_list = pushed_string_list->next;
  854.   free((char *)t);
  855. }
  856.  
  857. static void
  858. free_string_list ()
  859. {
  860.   register STRING_SAVER *t = pushed_string_list, *t1;
  861.  
  862.   while (t)
  863.     {
  864.       t1 = t->next;
  865.       if (t->saved_line)
  866.     free (t->saved_line);
  867.       if (t->saved_token_being_expanded)
  868.     free (t->saved_token_being_expanded);
  869.       free ((char *)t);
  870.       t = t1;
  871.     }
  872.   pushed_string_list = (STRING_SAVER *)NULL;
  873. }
  874.  
  875. /* This is a stack to save the values of all tokens for which alias
  876.    expansion has been performed during the current call to read_token ().
  877.    It is used to prevent alias expansion loops:
  878.  
  879.       alias foo=bar
  880.       alias bar=baz
  881.       alias baz=foo
  882.  
  883.    Ideally this would be taken care of by push and pop string, but because
  884.    of when strings are popped the stack will not contain the correct
  885.    strings to test against.  (The popping is done in shell_getc, so that when
  886.    the current string is exhausted, shell_getc can simply pop that string off
  887.    the stack, restore the previous string, and continue with the character
  888.    following the token whose expansion was originally pushed on the stack.)
  889.  
  890.    What we really want is a record of all tokens that have been expanded for
  891.    aliases during the `current' call to read_token().  This does that, at the
  892.    cost of being somewhat special-purpose (OK, OK vile and unclean).  Brian,
  893.    you had better rewrite this whole piece of garbage before the next version
  894.    is released.
  895. */
  896.  
  897. typedef struct _exp_saver {
  898.       struct _exp_saver *next;
  899.       char *saved_token;
  900. } EXPANSION_SAVER;
  901.  
  902. EXPANSION_SAVER *expanded_token_stack = (EXPANSION_SAVER *)NULL;
  903.  
  904. static void
  905. save_expansion (s)
  906.      char *s;
  907. {
  908.   EXPANSION_SAVER *t;
  909.  
  910.   t = (EXPANSION_SAVER *) xmalloc (sizeof (EXPANSION_SAVER));
  911.   t->saved_token = savestring (s);
  912.   t->next = expanded_token_stack;
  913.   expanded_token_stack = t;
  914. }
  915.  
  916. /*
  917.  * Return 1 if TOKEN has already been expanded in the current `stack' of
  918.  * expansions.  If it has been expanded already, it will appear as the value
  919.  * of saved_token for some entry in the stack of expansions created for the
  920.  * current token being expanded.
  921.  */
  922. static int
  923. token_has_been_expanded (token)
  924.      char *token;
  925. {
  926.   register EXPANSION_SAVER *t = expanded_token_stack;
  927.  
  928.   while (t)
  929.     {
  930.       if (STREQ (token, t->saved_token))
  931.     return (1);
  932.       t = t->next;
  933.     }
  934.   return (0);
  935. }
  936.  
  937. static void
  938. free_expansion_stack ()
  939. {
  940.   register EXPANSION_SAVER *t = expanded_token_stack, *t1;
  941.  
  942.   while (t)
  943.     {
  944.       t1 = t->next;
  945.       free (t->saved_token);
  946.       free (t);
  947.       t = t1;
  948.     }
  949.   expanded_token_stack = (EXPANSION_SAVER *)NULL;
  950. }
  951.  
  952. #endif /* ALIAS */
  953.  
  954. /* Return a line of text, taken from wherever yylex () reads input.
  955.    If there is no more input, then we return NULL. */
  956. char *
  957. read_a_line ()
  958. {
  959.   char *line_buffer = (char *)NULL;
  960.   int indx = 0, buffer_size = 0;
  961.   int c;
  962.  
  963.   while (1)
  964.     {
  965.       c = yy_getc ();
  966.  
  967.       if (c == 0)
  968.     continue;
  969.  
  970.       /* If there is no more input, then we return NULL. */
  971.       if (c == EOF)
  972.     {
  973.       c = '\n';
  974.       if (!line_buffer)
  975.         return ((char *)NULL);
  976.     }
  977.  
  978.       /* `+2' in case the final (200'th) character in the buffer is a newline;
  979.      otherwise the code below that NULL-terminates it will write over the
  980.      201st slot and kill the range checking in free(). */
  981.       if (indx + 2 > buffer_size)
  982.     if (!buffer_size)
  983.       line_buffer = (char *)xmalloc (buffer_size = 200);
  984.     else
  985.       line_buffer = (char *)xrealloc (line_buffer, buffer_size += 200);
  986.  
  987.       line_buffer[indx++] = c;
  988.       if (c == '\n')
  989.     {
  990.       line_buffer[indx] = '\0';
  991.       return (line_buffer);
  992.     }
  993.     }
  994. }
  995.  
  996. /* Return a line as in read_a_line (), but insure that the prompt is
  997.    the secondary prompt. */
  998. char *
  999. read_secondary_line ()
  1000. {
  1001.   prompt_string_pointer = &ps2_prompt;
  1002.   prompt_again ();
  1003.   return (read_a_line ());
  1004. }
  1005.  
  1006.  
  1007. /* **************************************************************** */
  1008. /*                                    */
  1009. /*                YYLEX ()                */
  1010. /*                                    */
  1011. /* **************************************************************** */
  1012.  
  1013. /* Reserved words.  These are only recognized as the first word of a
  1014.    command.  TOKEN_WORD_ALIST. */
  1015. STRING_INT_ALIST word_token_alist[] = {
  1016.   {"if", IF},
  1017.   {"then", THEN},
  1018.   {"else", ELSE},
  1019.   {"elif", ELIF},
  1020.   {"fi", FI},
  1021.   {"case", CASE},
  1022.   {"esac", ESAC},
  1023.   {"for", FOR},
  1024.   {"while", WHILE},
  1025.   {"until", UNTIL},
  1026.   {"do", DO},
  1027.   {"done", DONE},
  1028.   {"in", IN},
  1029.   {"function", FUNCTION},
  1030.   {"{", '{'},
  1031.   {"}", '}'},
  1032.   {"!", BANG},
  1033.   {(char *)NULL, 0}
  1034. };
  1035.  
  1036. /* Where shell input comes from.  History expansion is performed on each
  1037.    line, when the shell is interactive. */
  1038. char *shell_input_line = (char *)NULL;
  1039. int shell_input_line_index = 0;
  1040. int shell_input_line_size = 0;    /* amount allocated for shell_input_line */
  1041. int shell_input_line_len = 0;    /* strlen (shell_input_line) */
  1042.  
  1043. /* Either zero, or EOF. */
  1044. int shell_input_line_terminator = 0;
  1045.  
  1046. /* Return the next shell input character.  This always reads characters
  1047.    from shell_input_line; when that line is exhausted, it is time to
  1048.    read the next line. */
  1049. int
  1050. shell_getc (remove_quoted_newline)
  1051.      int remove_quoted_newline;
  1052. {
  1053.   extern int login_shell;
  1054.   int c;
  1055.  
  1056.   QUIT;    /* XXX this is experimental */
  1057.  
  1058. #if defined (ALIAS)
  1059.   /* If shell_input_line[shell_input_line_index] == 0, but there is
  1060.      something on the pushed list of strings, then we don't want to go
  1061.      off and get another line.  We let the code down below handle it. */
  1062.  
  1063.   if (!shell_input_line || ((!shell_input_line[shell_input_line_index]) &&
  1064.                 (pushed_string_list == (STRING_SAVER *)NULL)))
  1065. #else /* !ALIAS */
  1066.   if (!shell_input_line || !shell_input_line[shell_input_line_index])
  1067. #endif /* !ALIAS */
  1068.     {
  1069.       register int i, l;
  1070.       char *pre_process_line (), *expansions;
  1071.  
  1072.       restart_read_next_line:
  1073.  
  1074.       line_number++;
  1075.  
  1076.     restart_read:
  1077.  
  1078.       QUIT;    /* XXX experimental */
  1079.  
  1080.       i = 0;
  1081.       shell_input_line_terminator = 0;
  1082.  
  1083. #if defined (JOB_CONTROL)
  1084.       notify_and_cleanup ();
  1085. #endif
  1086.  
  1087.       clearerr (stdin);
  1088.       while (c = yy_getc ())
  1089.     {
  1090.       if (i + 2 > shell_input_line_size)
  1091.         shell_input_line = (char *)
  1092.           xrealloc (shell_input_line, shell_input_line_size += 256);
  1093.  
  1094.       if (c == EOF)
  1095.         {
  1096.           clearerr (stdin);
  1097.  
  1098.           if (!i)
  1099.         shell_input_line_terminator = EOF;
  1100.  
  1101.           shell_input_line[i] = '\0';
  1102.           break;
  1103.         }
  1104.  
  1105.       shell_input_line[i++] = c;
  1106.  
  1107.       if (c == '\n')
  1108.         {
  1109.           shell_input_line[--i] = '\0';
  1110.           break;
  1111.         }
  1112.     }
  1113.       shell_input_line_index = 0;
  1114.       shell_input_line_len = i;        /* == strlen (shell_input_line) */
  1115.  
  1116.       if (!shell_input_line[0])
  1117.     goto after_pre_process;
  1118.  
  1119.       if (interactive)
  1120.     {
  1121.       expansions = pre_process_line (shell_input_line, 1, 1);
  1122.  
  1123.       free (shell_input_line);
  1124.       shell_input_line = expansions;
  1125.       shell_input_line_len = shell_input_line ?
  1126.                  strlen (shell_input_line) :
  1127.                  0;
  1128.       /* We have to force the xrealloc below because we don't know the
  1129.          true allocated size of shell_input_line anymore. */
  1130.       shell_input_line_size = shell_input_line_len;
  1131.     }
  1132.  
  1133.   after_pre_process:
  1134.       if (shell_input_line)
  1135.     {
  1136.       if (echo_input_at_read)
  1137.         fprintf (stderr, "%s\n", shell_input_line);
  1138.     }
  1139.       else
  1140.     {
  1141.       shell_input_line_size = 0;
  1142.       prompt_string_pointer = ¤t_prompt_string;
  1143.       prompt_again ();
  1144.       goto restart_read;
  1145.     }
  1146.  
  1147.       /* Add the newline to the end of this string, iff the string does
  1148.      not already end in an EOF character.  */
  1149.       if (shell_input_line_terminator != EOF)
  1150.     {
  1151.       l = shell_input_line_len;    /* was a call to strlen */
  1152.  
  1153.       if (l + 3 > shell_input_line_size)
  1154.         shell_input_line = (char *)xrealloc (shell_input_line,
  1155.                     1 + (shell_input_line_size += 2));
  1156.  
  1157.       strcpy (shell_input_line + l, "\n");
  1158.     }
  1159.     }
  1160.   
  1161.   c = shell_input_line[shell_input_line_index];
  1162.  
  1163.   if (c)
  1164.     shell_input_line_index++;
  1165.  
  1166.   if (c == '\\' && remove_quoted_newline &&
  1167.       shell_input_line[shell_input_line_index] == '\n')
  1168.     {
  1169.     prompt_again ();
  1170.     goto restart_read_next_line;
  1171.     }
  1172.  
  1173. #if defined (ALIAS)
  1174.   /*
  1175.    * If c is NULL, we have reached the end of the current input string.  If
  1176.    * pushed_string_list is non-empty, it's time to pop to the previous string
  1177.    * because we have fully consumed the result of the last alias expansion.
  1178.    * Do it transparently; just return the next character of the string popped
  1179.    * to.  We need to hang onto current_token_being_expanded until the token
  1180.    * currently being read has been recognized; we can't restore it in
  1181.    * pop_string () because the token currently being read would be
  1182.    * inappropriately compared with it.  We defer restoration until the next
  1183.    * call to read_token ().
  1184.    */
  1185.  
  1186.   if (!c && (pushed_string_list != (STRING_SAVER *)NULL))
  1187.     {
  1188.       pop_string ();
  1189.       c = shell_input_line[shell_input_line_index];
  1190.       if (c)
  1191.     shell_input_line_index++;
  1192.     }
  1193. #endif /* ALIAS */
  1194.  
  1195.   if (!c && shell_input_line_terminator == EOF)
  1196.     {
  1197.       if (shell_input_line_index != 0)
  1198.     return ('\n');
  1199.       else
  1200.     return (EOF);
  1201.     }
  1202.  
  1203.   return (c);
  1204. }
  1205.  
  1206. /* Put C back into the input for the shell. */
  1207. shell_ungetc (c)
  1208.      int c;
  1209. {
  1210.   if (shell_input_line && shell_input_line_index)
  1211.     shell_input_line[--shell_input_line_index] = c;
  1212. }
  1213.  
  1214. /* Discard input until CHARACTER is seen. */
  1215. discard_until (character)
  1216.      int character;
  1217. {
  1218.   int c;
  1219.   while ((c = shell_getc (0)) != EOF && c != character)
  1220.     ;
  1221.   if (c != EOF )
  1222.     shell_ungetc (c);
  1223. }
  1224.  
  1225. #if defined (HISTORY_REEDITING)
  1226. /* Tell readline () that we have some text for it to edit. */
  1227. re_edit (text)
  1228.      char *text;
  1229. {
  1230. #if defined (READLINE)
  1231.   if (strcmp (stream_name, "readline stdin") == 0)
  1232.     bash_re_edit (text);
  1233. #endif /* READLINE */
  1234. }
  1235. #endif /* HISTORY_REEDITING */
  1236.  
  1237. /* Non-zero means do no history expansion on this line, regardless
  1238.    of what history_expansion says. */
  1239. int history_expansion_inhibited = 0;
  1240.  
  1241. /* Do pre-processing on LINE.  If PRINT_CHANGES is non-zero, then
  1242.    print the results of expanding the line if there were any changes.
  1243.    If there is an error, return NULL, otherwise the expanded line is
  1244.    returned.  If ADDIT is non-zero the line is added to the history
  1245.    list after history expansion.  ADDIT is just a suggestion;
  1246.    REMEMBER_ON_HISTORY can veto, and does.
  1247.    Right now this does history expansion. */
  1248. char *
  1249. pre_process_line (line, print_changes, addit)
  1250.      char *line;
  1251.      int print_changes, addit;
  1252. {
  1253.   extern int remember_on_history;
  1254.   extern int history_expansion;
  1255.   extern int history_expand ();
  1256.   char *history_value;
  1257.   char *return_value;
  1258.   int expanded = 0;
  1259.  
  1260.   return_value = line;
  1261.  
  1262.   /* History expand the line.  If this results in no errors, then
  1263.      add that line to the history if ADDIT is non-zero. */
  1264.   if (!history_expansion_inhibited && history_expansion)
  1265.     {
  1266.       expanded = history_expand (line, &history_value);
  1267.  
  1268.       if (expanded)
  1269.     {
  1270.       if (print_changes)
  1271.         fprintf (stderr, "%s\n", history_value);
  1272.  
  1273.       /* If there was an error, return NULL. */
  1274.       if (expanded < 0)
  1275.         {
  1276.           free (history_value);
  1277.  
  1278. #if defined (HISTORY_REEDITING)
  1279.           /* New hack.  We can allow the user to edit the
  1280.          failed history expansion. */
  1281.           re_edit (line);
  1282. #endif /* HISTORY_REEDITING */
  1283.           return ((char *)NULL);
  1284.         }
  1285.     }
  1286.  
  1287.       /* Let other expansions know that return_value can be free'ed,
  1288.      and that a line has been added to the history list.  Note
  1289.      that we only add lines that have something in them. */
  1290.       expanded = 1;
  1291.       return_value = history_value;
  1292.     }
  1293.  
  1294.   if (addit && remember_on_history && *return_value)
  1295.     {
  1296.       extern int history_control;
  1297.       extern int history_lines_this_session;
  1298.  
  1299.       switch (history_control)
  1300.     {
  1301.       case 0:
  1302.         add_history (return_value);
  1303.         history_lines_this_session++;
  1304.         break;
  1305.       case 1:
  1306.         if (*return_value != ' ')
  1307.           {
  1308.         add_history (return_value);
  1309.         history_lines_this_session++;
  1310.           }
  1311.         break;
  1312.       case 2:
  1313.         {
  1314.           HIST_ENTRY *temp;
  1315.  
  1316.           using_history ();
  1317.           temp = previous_history ();
  1318.           if (!temp || (strcmp (temp->line, return_value) != 0))
  1319.         {
  1320.           add_history (return_value);
  1321.           history_lines_this_session++;
  1322.         }
  1323.           using_history ();
  1324.         }
  1325.         break;
  1326.     }
  1327.     }
  1328.  
  1329.   if (!expanded)
  1330.     return_value = savestring (line);
  1331.  
  1332.   return (return_value);
  1333. }
  1334.  
  1335.  
  1336. /* Place to remember the token.  We try to keep the buffer
  1337.    at a reasonable size, but it can grow. */
  1338. char *token = (char *)NULL;
  1339.  
  1340. /* Current size of the token buffer. */
  1341. int token_buffer_size = 0;
  1342.  
  1343. /* Command to read_token () explaining what we want it to do. */
  1344. #define READ 0
  1345. #define RESET 1
  1346. #define prompt_is_ps1 \
  1347.       (!prompt_string_pointer || prompt_string_pointer == &ps1_prompt)
  1348.  
  1349. /* Function for yyparse to call.  yylex keeps track of
  1350.    the last two tokens read, and calls read_token.  */
  1351.  
  1352. yylex ()
  1353. {
  1354.   if (interactive && (!current_token || current_token == '\n'))
  1355.     {
  1356.       /* Before we print a prompt, we might have to check mailboxes.
  1357.      We do this only if it is time to do so. Notice that only here
  1358.      is the mail alarm reset; nothing takes place in check_mail ()
  1359.      except the checking of mail.  Please don't change this. */
  1360.       if (prompt_is_ps1 && time_to_check_mail ())
  1361.     {
  1362.       check_mail ();
  1363.       reset_mail_timer ();
  1364.     }
  1365.  
  1366.       /* Allow the execution of a random command just before the printing
  1367.      of each primary prompt.  If the shell variable PROMPT_COMMAND
  1368.      is set then the value of it is the command to execute. */
  1369.       if (prompt_is_ps1)
  1370.     {
  1371.       char *command_to_execute = get_string_value ("PROMPT_COMMAND");
  1372.  
  1373.       if (command_to_execute)
  1374.         {
  1375.           extern Function *last_shell_builtin, *this_shell_builtin;
  1376.           extern int last_command_exit_value;
  1377.           Function *temp_last, *temp_this;
  1378.           int temp_exit_value, temp_eof_encountered;
  1379.  
  1380.           temp_last = last_shell_builtin;
  1381.           temp_this = this_shell_builtin;
  1382.           temp_exit_value = last_command_exit_value;
  1383.           temp_eof_encountered = eof_encountered;
  1384.  
  1385.           parse_and_execute
  1386.         (savestring (command_to_execute), "PROMPT_COMMAND");
  1387.  
  1388.           last_shell_builtin = temp_last;
  1389.           this_shell_builtin = temp_this;
  1390.           last_command_exit_value = temp_exit_value;
  1391.           eof_encountered = temp_eof_encountered;
  1392.         }
  1393.     }
  1394.       prompt_again ();
  1395.     }
  1396.  
  1397.   token_before_that = last_read_token;
  1398.   last_read_token = current_token;
  1399.   current_token = read_token (READ);
  1400.   return (current_token);
  1401. }
  1402.  
  1403. /* Called from shell.c when Control-C is typed at top level.  Or
  1404.    by the error rule at top level. */
  1405. reset_parser ()
  1406. {
  1407.   read_token (RESET);
  1408. }
  1409.   
  1410. /* When non-zero, we have read the required tokens
  1411.    which allow ESAC to be the next one read. */
  1412. static int allow_esac_as_next = 0;
  1413.  
  1414. /* When non-zero, accept single '{' as a token itself. */
  1415. static int allow_open_brace = 0;
  1416.  
  1417. /* DELIMITER is the value of the delimiter that is currently
  1418.    enclosing, or zero for none. */
  1419. static int delimiter = 0;
  1420. static int old_delimiter = 0;
  1421.  
  1422. /* When non-zero, an open-brace used to create a group is awaiting a close
  1423.    brace partner. */
  1424. static int open_brace_awaiting_satisfaction = 0;
  1425.  
  1426. /* If non-zero, it is the token that we want read_token to return regardless
  1427.    of what text is (or isn't) present to be read.  read_token resets this. */
  1428. int token_to_read = 0;
  1429.  
  1430. /* Read the next token.  Command can be READ (normal operation) or 
  1431.    RESET (to normalize state). */
  1432. read_token (command)
  1433.      int command;
  1434. {
  1435.   extern int interactive_shell;    /* Whether the current shell is interactive. */
  1436.   int character;        /* Current character. */
  1437.   int peek_char;        /* Temporary look-ahead character. */
  1438.   int result;            /* The thing to return. */
  1439.   WORD_DESC *the_word;        /* The value for YYLVAL when a WORD is read. */
  1440.  
  1441.   if (token_buffer_size < TOKEN_DEFAULT_GROW_SIZE)
  1442.     {
  1443.       if (token)
  1444.     free (token);
  1445.       token = (char *)xmalloc (token_buffer_size = TOKEN_DEFAULT_GROW_SIZE);
  1446.     }
  1447.  
  1448.   if (command == RESET)
  1449.     {
  1450.       delimiter = old_delimiter = 0;
  1451.       open_brace_awaiting_satisfaction = 0;
  1452.       in_case_pattern_list = 0;
  1453.  
  1454. #if defined (ALIAS)
  1455.       if (pushed_string_list)
  1456.     {
  1457.       free_string_list ();
  1458.       pushed_string_list = (STRING_SAVER *)NULL;
  1459.     }
  1460.  
  1461.       if (pending_token_being_expanded)
  1462.     {
  1463.       free (pending_token_being_expanded);
  1464.       pending_token_being_expanded = (char *)NULL;
  1465.     }
  1466.  
  1467.       if (current_token_being_expanded)
  1468.     {
  1469.       free (current_token_being_expanded);
  1470.       current_token_being_expanded = (char *)NULL;
  1471.     }
  1472.  
  1473.       if (expanded_token_stack)
  1474.     {
  1475.       free_expansion_stack ();
  1476.       expanded_token_stack = (EXPANSION_SAVER *)NULL;
  1477.     }
  1478.  
  1479.       expand_next_token = 0;
  1480. #endif /* ALIAS */
  1481.  
  1482.       if (shell_input_line)
  1483.     {
  1484.       free (shell_input_line);
  1485.       shell_input_line = (char *)NULL;
  1486.       shell_input_line_size = shell_input_line_index = 0;
  1487.     }
  1488.       last_read_token = '\n';
  1489.       token_to_read = '\n';
  1490.       return ('\n');
  1491.     }
  1492.  
  1493.   if (token_to_read)
  1494.     {
  1495.       int rt = token_to_read;
  1496.       token_to_read = 0;
  1497.       return (rt);
  1498.     }
  1499.  
  1500. #if defined (ALIAS)
  1501.   /*
  1502.    * Now we can replace current_token_being_expanded with 
  1503.    * pending_token_being_expanded, since the token that would be 
  1504.    * inappropriately compared has already been returned.
  1505.    *
  1506.    * To see why restoring current_token_being_expanded in pop_string ()
  1507.    * could be a problem, consider "alias foo=foo".  Then try to
  1508.    * expand `foo'.  The initial value of current_token_being_expanded is
  1509.    * NULL, so that is what is pushed onto pushed_string_list as the
  1510.    * value of saved_token_being_expanded.  "foo" then becomes shell_input_line.
  1511.    * read_token calls shell_getc for `f', `o', `o', and then shell_getc
  1512.    * hits the end of shell_input_line.  pushed_string_list is not empty
  1513.    * so it gets popped.  If we were to blindly restore
  1514.    * current_token_being_expanded at this point, `foo' would be compared
  1515.    * with a NULL string in the check for recursive expansion, and would
  1516.    * infinitely recurse.
  1517.    */
  1518.   if (pending_token_being_expanded)
  1519.     {
  1520.       if (current_token_being_expanded)
  1521.     free (current_token_being_expanded);
  1522.       current_token_being_expanded = pending_token_being_expanded;
  1523.       pending_token_being_expanded = (char *)NULL;
  1524.     }
  1525.  
  1526.   /* If we hit read_token () and there are no saved strings on the
  1527.      pushed_string_list, then we are no longer currently expanding a
  1528.      token.  This can't be done in pop_stream, because pop_stream
  1529.      may pop the stream before the current token has finished being
  1530.      completely expanded (consider what happens when we alias foo to foo,
  1531.      and then try to expand it). */
  1532.   if (!pushed_string_list && current_token_being_expanded)
  1533.     {
  1534.       free (current_token_being_expanded);
  1535.       current_token_being_expanded = (char *)NULL;
  1536.  
  1537.       if (expanded_token_stack)
  1538.     {
  1539.       free_expansion_stack ();
  1540.       expanded_token_stack = (EXPANSION_SAVER *)NULL;
  1541.     }
  1542.     }
  1543.  
  1544.   /* This is a place to jump back to once we have successfully expanded a
  1545.      token with an alias and pushed the string with push_string () */
  1546. re_read_token:
  1547.  
  1548. #endif /* ALIAS */
  1549.  
  1550.   /* Read a single word from input.  Start by skipping blanks. */
  1551.   while ((character = shell_getc (1)) != EOF && whitespace (character));
  1552.  
  1553.   if (character == EOF)
  1554.     return (yacc_EOF);
  1555.  
  1556.   if (character == '#' && !interactive)
  1557.     {
  1558.       /* A comment.  Discard until EOL or EOF, and then return a newline. */
  1559.       discard_until ('\n');
  1560.       shell_getc (0);
  1561.  
  1562.       /* If we're about to return an unquoted newline, we can go and collect
  1563.      the text of any pending here document. */
  1564.       if (need_here_doc)
  1565.     make_here_document (redirection_needing_here_doc);
  1566.       need_here_doc = 0;
  1567.  
  1568. #if defined (ALIAS)
  1569.       expand_next_token = 0;
  1570. #endif /* ALIAS */
  1571.  
  1572.       return ('\n');
  1573.     }
  1574.  
  1575.   if (character == '\n')
  1576.     {
  1577.       /* If we're about to return an unquoted newline, we can go and collect
  1578.      the text of any pending here document. */
  1579.       if (need_here_doc)
  1580.     make_here_document (redirection_needing_here_doc);
  1581.       need_here_doc = 0;
  1582.  
  1583. #if defined (ALIAS)
  1584.       expand_next_token = 0;
  1585. #endif /* ALIAS */
  1586.  
  1587.       return (character);
  1588.     }
  1589.  
  1590.   if (member (character, "()<>;&|"))
  1591.     {
  1592. #if defined (ALIAS)
  1593.       /* Turn off alias tokenization iff this character sequence would
  1594.      not leave us ready to read a command. */
  1595.       if (character == '<' || character == '>')
  1596.     expand_next_token = 0;
  1597. #endif /* ALIAS */
  1598.  
  1599.       /* Please note that the shell does not allow whitespace to
  1600.      appear in between tokens which are character pairs, such as
  1601.      "<<" or ">>".  I believe this is the correct behaviour. */
  1602.       if (character == (peek_char = shell_getc (1)))
  1603.     {
  1604.       switch (character)
  1605.         {
  1606.           /* If '<' then we could be at "<<" or at "<<-".  We have to
  1607.          look ahead one more character. */
  1608.         case '<':
  1609.           peek_char = shell_getc (1);
  1610.           if (peek_char == '-')
  1611.         return (LESS_LESS_MINUS);
  1612.           else
  1613.         {
  1614.           shell_ungetc (peek_char);
  1615.           return (LESS_LESS);
  1616.         }
  1617.  
  1618.         case '>':
  1619.           return (GREATER_GREATER);
  1620.  
  1621.         case ';':
  1622.           in_case_pattern_list = 1;
  1623. #if defined (ALIAS)
  1624.           expand_next_token = 0;
  1625. #endif /* ALIAS */
  1626.           return (SEMI_SEMI);
  1627.  
  1628.         case '&':
  1629.           return (AND_AND);
  1630.  
  1631.         case '|':
  1632.           return (OR_OR);
  1633.         }
  1634.     }
  1635.       else
  1636.     {
  1637.       if (peek_char == '&')
  1638.         {
  1639.           switch (character)
  1640.         {
  1641.         case '<': return (LESS_AND);
  1642.         case '>': return (GREATER_AND);
  1643.         }
  1644.         }
  1645.       if (character == '<' && peek_char == '>')
  1646.         return (LESS_GREATER);
  1647.       if (character == '>' && peek_char == '|')
  1648.         return (GREATER_BAR);
  1649.       if (peek_char == '>' && character == '&')
  1650.         return (AND_GREATER);
  1651.     }
  1652.       shell_ungetc (peek_char);
  1653.  
  1654.       /* If we look like we are reading the start of a function
  1655.      definition, then let the reader know about it so that
  1656.      we will do the right thing with `{'. */
  1657.       if (character == ')' &&
  1658.       last_read_token == '(' && token_before_that == WORD)
  1659.     {
  1660.       allow_open_brace = 1;
  1661. #if defined (ALIAS)
  1662.       expand_next_token = 0;
  1663. #endif /* ALIAS */
  1664.     }
  1665.  
  1666.       if (in_case_pattern_list && (character == ')'))
  1667.     in_case_pattern_list = 0;
  1668.  
  1669.       return (character);
  1670.     }
  1671.  
  1672.   /* Hack <&- (close stdin) case. */
  1673.   if (character == '-')
  1674.     {
  1675.       switch (last_read_token)
  1676.     {
  1677.     case LESS_AND:
  1678.     case GREATER_AND:
  1679.       return (character);
  1680.     }
  1681.     }
  1682.   
  1683.   /* Okay, if we got this far, we have to read a word.  Read one,
  1684.      and then check it against the known ones. */
  1685.   {
  1686.     /* Index into the token that we are building. */
  1687.     int token_index = 0;
  1688.  
  1689.     /* ALL_DIGITS becomes zero when we see a non-digit. */
  1690.     int all_digits = digit (character);
  1691.  
  1692.     /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
  1693.     int dollar_present = 0;
  1694.  
  1695.     /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
  1696.     int quoted = 0;
  1697.  
  1698.     /* Non-zero means to ignore the value of the next character, and just
  1699.        to add it no matter what. */
  1700.     int pass_next_character = 0;
  1701.  
  1702.     /* Non-zero means parsing a dollar-paren construct.  It is the count of
  1703.        un-quoted closes we need to see. */
  1704.     int dollar_paren_level = 0;
  1705.  
  1706.     /* Non-zero means parsing a dollar-bracket construct ($[...]).  It is
  1707.        the count of un-quoted `]' characters we need to see. */
  1708.     int dollar_bracket_level = 0;
  1709.  
  1710.     /* Another level variable.  This one is for dollar_parens inside of
  1711.        double-quotes. */
  1712.     int delimited_paren_level = 0;
  1713.  
  1714.     for (;;)
  1715.       {
  1716.     if (character == EOF)
  1717.       goto got_token;
  1718.  
  1719.     if (pass_next_character)
  1720.       {
  1721.         pass_next_character = 0;
  1722.         goto got_character;
  1723.       }
  1724.  
  1725.       if (delimiter && character == '\\' && delimiter != '\'')
  1726.     {
  1727.       peek_char = shell_getc (0);
  1728.       if (peek_char != '\\')
  1729.         shell_ungetc (peek_char);
  1730.       else
  1731.         {
  1732.           token[token_index++] = character;
  1733.           goto got_character;
  1734.         }
  1735.     }
  1736.  
  1737.     /* Handle backslashes.  Quote lots of things when not inside of
  1738.        double-quotes, quote some things inside of double-quotes. */
  1739.        
  1740.     if (character == '\\' && delimiter != '\'')
  1741.       {
  1742.         peek_char = shell_getc (0);
  1743.  
  1744.         /* Backslash-newline is ignored in all cases excepting
  1745.            when quoted with single quotes. */
  1746.         if (peek_char == '\n')
  1747.           {
  1748.         character = '\n';
  1749.         goto next_character;
  1750.           }
  1751.         else
  1752.           {
  1753.         shell_ungetc (peek_char);
  1754.  
  1755.         /* If the next character is to be quoted, do it now. */
  1756.         if (!delimiter || delimiter == '`' ||
  1757.             ((delimiter == '"' ) &&
  1758.              (member (peek_char, slashify_in_quotes))))
  1759.           {
  1760.             pass_next_character++;
  1761.             quoted = 1;
  1762.             goto got_character;
  1763.           }
  1764.           }
  1765.       }
  1766.  
  1767.     /* This is a hack, in its present form.  If a backquote substitution
  1768.        appears within double quotes, everything within the backquotes
  1769.        should be read as part of a single word.  Jesus.  Now I see why
  1770.        Korn introduced the $() form. */
  1771.     if (delimiter && delimiter == '"' && character == '`')
  1772.       {
  1773.         old_delimiter = delimiter;
  1774.         delimiter = character;
  1775.         goto got_character;
  1776.       }
  1777.  
  1778.     if (delimiter)
  1779.       {
  1780.         if (character == delimiter)
  1781.           {
  1782.         if (delimited_paren_level)
  1783.           {
  1784. #if defined (NOTDEF)
  1785.             report_error ("Expected ')' before %c", character);
  1786.             return ('\n');
  1787. #else
  1788.             goto got_character;
  1789. #endif /* NOTDEF */
  1790.           }
  1791.  
  1792.         delimiter = 0;
  1793.  
  1794.         if (old_delimiter == '"' && character == '`')
  1795.           {
  1796.             delimiter = old_delimiter;
  1797.             old_delimiter = 0;
  1798.           }
  1799.  
  1800.         goto got_character;
  1801.           }
  1802.       }
  1803.  
  1804.     if (!delimiter || delimiter == '`' || delimiter == '"')
  1805.       {
  1806.         if (character == '$')
  1807.           {
  1808.         peek_char = shell_getc (1);
  1809.         shell_ungetc (peek_char);
  1810.         if (peek_char == '(')
  1811.           {
  1812.             if (!delimiter)
  1813.               dollar_paren_level++;
  1814.             else
  1815.               delimited_paren_level++;
  1816.  
  1817.             pass_next_character++;
  1818.             goto got_character;
  1819.           }
  1820.         else if (peek_char == '[')
  1821.           {
  1822.             if (!delimiter)
  1823.               dollar_bracket_level++;
  1824.  
  1825.             pass_next_character++;
  1826.             goto got_character;
  1827.           }
  1828.           }
  1829.  
  1830.         /* If we are parsing a $() or $[] construct, we need to balance
  1831.            parens and brackets inside the construct.  This whole function
  1832.            could use a rewrite. */
  1833.         if (character == '(')
  1834.           {
  1835.         if (delimiter && delimited_paren_level)
  1836.           delimited_paren_level++;
  1837.  
  1838.         if (!delimiter && dollar_paren_level)
  1839.           dollar_paren_level++;
  1840.           }
  1841.  
  1842.         if (character == '[')
  1843.           {
  1844.         if (!delimiter && dollar_bracket_level)
  1845.           dollar_bracket_level++;
  1846.           }
  1847.  
  1848.         /* This code needs to take into account whether we are inside a
  1849.            case statement pattern list, and whether this paren is supposed
  1850.            to terminate it (hey, it could happen).  It's not as simple
  1851.            as just using in_case_pattern_list, because we're not parsing
  1852.            anything while we're reading a $( ) construct.  Maybe we
  1853.            should move that whole mess into the yacc parser. */
  1854.         if (character == ')')
  1855.           {
  1856.         if (delimiter && delimited_paren_level)
  1857.           delimited_paren_level--;
  1858.  
  1859.         if (!delimiter && dollar_paren_level)
  1860.           {
  1861.             dollar_paren_level--;
  1862.             goto got_character;
  1863.           }
  1864.           }
  1865.  
  1866.         if (character == ']')
  1867.           {
  1868.         if (!delimiter && dollar_bracket_level)
  1869.           {
  1870.             dollar_bracket_level--;
  1871.             goto got_character;
  1872.           }
  1873.           }
  1874.       }
  1875.  
  1876.     if (!dollar_paren_level && !dollar_bracket_level && !delimiter &&
  1877.         member (character, " \t\n;&()|<>"))
  1878.       {
  1879.         shell_ungetc (character);
  1880.         goto got_token;
  1881.       }
  1882.     
  1883.     if (!delimiter)
  1884.       {
  1885.         if (character == '"' || character == '`' || character == '\'')
  1886.           {
  1887.         quoted = 1;
  1888.         delimiter = character;
  1889.         goto got_character;
  1890.           }
  1891.       }
  1892.  
  1893.     if (all_digits) all_digits = digit (character);
  1894.     if (character == '$') dollar_present = 1;
  1895.  
  1896.       got_character:
  1897.  
  1898.     token[token_index++] = character;
  1899.  
  1900.     if (token_index == (token_buffer_size - 1))
  1901.       token = (char *)xrealloc (token, (token_buffer_size
  1902.                         += TOKEN_DEFAULT_GROW_SIZE));
  1903.     {
  1904.       char *decode_prompt_string ();
  1905.  
  1906.     next_character:
  1907.       if (character == '\n' && interactive && yy_input_type != st_string)
  1908.         prompt_again ();
  1909.     }
  1910.     /* We want to remove quoted newlines (that is, a \<newline> pair)
  1911.        unless we are within single quotes or pass_next_character is
  1912.        set (the shell equivalent of literal-next). */
  1913.     character = shell_getc ((delimiter != '\'') && (!pass_next_character));
  1914.       }
  1915.  
  1916.   got_token:
  1917.  
  1918.     token[token_index] = '\0';
  1919.     
  1920.     if ((delimiter || dollar_paren_level || dollar_bracket_level) &&
  1921.     character == EOF)
  1922.       {
  1923.     if (dollar_paren_level && !delimiter)
  1924.       delimiter = ')';
  1925.     else if (dollar_bracket_level && !delimiter)
  1926.       delimiter = ']';
  1927.  
  1928.     report_error ("Unexpected EOF.  Looking for `%c'.", delimiter);
  1929.     return (-1);
  1930.       }
  1931.  
  1932.     if (all_digits)
  1933.       {
  1934.     /* Check to see what thing we should return.  If the last_read_token
  1935.        is a `<', or a `&', or the character which ended this token is
  1936.        a '>' or '<', then, and ONLY then, is this input token a NUMBER.
  1937.        Otherwise, it is just a word, and should be returned as such. */
  1938.  
  1939.     if ((character == '<' || character == '>') ||
  1940.         (last_read_token == LESS_AND ||
  1941.          last_read_token == GREATER_AND))
  1942.       {
  1943.         yylval.number = atoi (token); /* was sscanf (token, "%d", &(yylval.number)); */
  1944.         return (NUMBER);
  1945.       }
  1946.       }
  1947.  
  1948.     /* Handle special case.  IN is recognized if the last token
  1949.        was WORD and the token before that was FOR or CASE. */
  1950.     if ((last_read_token == WORD) &&
  1951.     ((token_before_that == FOR) || (token_before_that == CASE)) &&
  1952.     (STREQ (token, "in")))
  1953.       {
  1954.     if (token_before_that == CASE)
  1955.       {
  1956.         in_case_pattern_list = 1;
  1957.         allow_esac_as_next++;
  1958.       }
  1959.     return (IN);
  1960.       }
  1961.  
  1962.     /* Ditto for DO in the FOR case. */
  1963.     if ((last_read_token == WORD) && (token_before_that == FOR) &&
  1964.     (STREQ (token, "do")))
  1965.       return (DO);
  1966.  
  1967.     /* Ditto for ESAC in the CASE case. 
  1968.        Specifically, this handles "case word in esac", which is a legal
  1969.        construct, certainly because someone will pass an empty arg to the
  1970.        case construct, and we don't want it to barf.  Of course, we should
  1971.        insist that the case construct has at least one pattern in it, but
  1972.        the designers disagree. */
  1973.     if (allow_esac_as_next)
  1974.       {
  1975.     allow_esac_as_next--;
  1976.     if (STREQ (token, "esac"))
  1977.       {
  1978.         in_case_pattern_list = 0;
  1979.         return (ESAC);
  1980.       }
  1981.       }
  1982.  
  1983.     /* Ditto for `{' in the FUNCTION case. */
  1984.     if (allow_open_brace)
  1985.       {
  1986.     allow_open_brace = 0;
  1987.     if (STREQ (token, "{"))
  1988.       {
  1989.         open_brace_awaiting_satisfaction++;
  1990.         return ('{');
  1991.       }
  1992.       }
  1993.  
  1994. #if defined (ALIAS)
  1995.  
  1996. #define command_token_position(token) \
  1997.     ((token) != SEMI_SEMI && reserved_word_acceptable (token))
  1998.  
  1999.     /* OK, we have a token.  Let's try to alias expand it, if (and only if)
  2000.        it's eligible. 
  2001.  
  2002.        It is eligible for expansion if the shell is in interactive mode, and
  2003.        the token is unquoted and the last token read was a command
  2004.        separator (or expand_next_token is set), and we are currently
  2005.        processing an alias (pushed_string_list is non-empty) and this
  2006.        token is not the same as the current or any previously
  2007.        processed alias.
  2008.  
  2009.        Special cases that disqualify:
  2010.      In a pattern list in a case statement (in_case_pattern_list). */
  2011.     if (interactive_shell && !quoted && !in_case_pattern_list &&
  2012.     (command_token_position (last_read_token) || expand_next_token))
  2013.       {
  2014.     char *alias_expand_word (), *expanded;
  2015.     if (current_token_being_expanded &&
  2016.          ((STREQ (token, current_token_being_expanded)) ||
  2017.           (token_has_been_expanded (token))))
  2018.       goto no_expansion;
  2019.  
  2020.     expanded = alias_expand_word (token);
  2021.     if (expanded)
  2022.       {
  2023.         int len = strlen (expanded), expand_next;
  2024.         char *temp;
  2025.  
  2026.         /* Erase the current token. */
  2027.         token_index = 0;
  2028.  
  2029.         expand_next = (expanded[len - 1] == ' ') ||
  2030.               (expanded[len - 1] == '\t');
  2031.  
  2032.         temp = savestring (token);
  2033.         push_string (expanded, expand_next, temp);
  2034.         goto re_read_token;
  2035.       }
  2036.     else
  2037.       /* This is an eligible token that does not have an expansion. */
  2038. no_expansion:
  2039.       expand_next_token = 0;
  2040.       }
  2041.     else
  2042.       {
  2043.     expand_next_token = 0;
  2044.       }
  2045. #endif /* ALIAS */
  2046.  
  2047.     /* Check to see if it is a reserved word.  */
  2048.     if (!dollar_present && !quoted &&
  2049.     reserved_word_acceptable (last_read_token))
  2050.       {
  2051.     int i;
  2052.     for (i = 0; word_token_alist[i].word != (char *)NULL; i++)
  2053.       if (STREQ (token, word_token_alist[i].word))
  2054.         {
  2055.           if (in_case_pattern_list && (word_token_alist[i].token != ESAC))
  2056.         break;
  2057.  
  2058.           if (word_token_alist[i].token == ESAC)
  2059.         in_case_pattern_list = 0;
  2060.  
  2061.           if (word_token_alist[i].token == '{')
  2062.         open_brace_awaiting_satisfaction++;
  2063.  
  2064.           return (word_token_alist[i].token);
  2065.         }
  2066.       }
  2067.  
  2068.     /* What if we are attempting to satisfy an open-brace grouper? */
  2069.     if (open_brace_awaiting_satisfaction && strcmp (token, "}") == 0)
  2070.       {
  2071.     open_brace_awaiting_satisfaction--;
  2072.     return ('}');
  2073.       }
  2074.  
  2075.     the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
  2076.     the_word->word = (char *)xmalloc (1 + strlen (token));
  2077.     strcpy (the_word->word, token);
  2078.     the_word->dollar_present = dollar_present;
  2079.     the_word->quoted = quoted;
  2080.     the_word->assignment = assignment (token);
  2081.  
  2082.     yylval.word = the_word;
  2083.     result = WORD;
  2084.     if (last_read_token == FUNCTION)
  2085.       allow_open_brace = 1;
  2086.   }
  2087.   return (result);
  2088. }
  2089.  
  2090. #if defined (NOTDEF)        /* Obsoleted function no longer used. */
  2091. /* Return 1 if this token is a legal shell `identifier'; that is, it consists
  2092.    solely of letters, digits, and underscores, and does not begin with a 
  2093.    digit. */
  2094. legal_identifier (name)
  2095.      char *name;
  2096. {
  2097.   register char *s;
  2098.  
  2099.   if (!name || !*name)
  2100.     return (0);
  2101.  
  2102.   if (digit (*name))
  2103.     return (0);
  2104.  
  2105.   for (s = name; s && *s; s++)
  2106.     {
  2107.       if (!isletter (*s) && !digit (*s) && (*s != '_'))
  2108.     return (0);
  2109.     }
  2110.   return (1);
  2111. }
  2112. #endif /* NOTDEF */
  2113.  
  2114. /* Return 1 if TOKEN is a token that after being read would allow
  2115.    a reserved word to be seen, else 0. */
  2116. static int
  2117. reserved_word_acceptable (token)
  2118.      int token;
  2119. {
  2120.   if (member (token, "\n;()|&{") ||
  2121.       token == AND_AND ||
  2122.       token == BANG ||
  2123.       token == DO ||
  2124.       token == ELIF ||
  2125.       token == ELSE ||
  2126.       token == IF ||
  2127.       token == OR_OR ||
  2128.       token == SEMI_SEMI ||
  2129.       token == THEN ||
  2130.       token == UNTIL ||
  2131.       token == WHILE ||
  2132.       token == 0)
  2133.     return (1);
  2134.   else
  2135.     return (0);
  2136. }
  2137.  
  2138. #if defined (READLINE)
  2139. /* Called after each time readline is called.  This insures that whatever
  2140.    the new prompt string is gets propagated to readline's local prompt
  2141.    variable. */
  2142. reset_readline_prompt ()
  2143. {
  2144.   if (prompt_string_pointer && *prompt_string_pointer)
  2145.     {
  2146.       char *temp_prompt, *decode_prompt_string ();
  2147.  
  2148.       temp_prompt = decode_prompt_string (*prompt_string_pointer);
  2149.  
  2150.       if (!temp_prompt)
  2151.     temp_prompt = savestring ("");
  2152.  
  2153.       if (current_readline_prompt)
  2154.     free (current_readline_prompt);
  2155.  
  2156.       current_readline_prompt = temp_prompt;
  2157.     }
  2158. }
  2159. #endif
  2160.  
  2161. /* Issue a prompt, or prepare to issue a prompt when the next character
  2162.    is read. */
  2163. prompt_again ()
  2164. {
  2165.   char *temp_prompt, *decode_prompt_string ();
  2166.  
  2167.   ps1_prompt = get_string_value ("PS1");
  2168.   ps2_prompt = get_string_value ("PS2");
  2169.  
  2170.   if (!prompt_string_pointer)
  2171.     prompt_string_pointer = &ps1_prompt;
  2172.  
  2173.   if (*prompt_string_pointer)
  2174.     temp_prompt = decode_prompt_string (*prompt_string_pointer);
  2175.   else
  2176.     temp_prompt = savestring ("");
  2177.  
  2178.   current_prompt_string = *prompt_string_pointer;
  2179.   prompt_string_pointer = &ps2_prompt;
  2180.  
  2181. #if defined (READLINE)
  2182.   if (!no_line_editing)
  2183.     {
  2184.       if (current_readline_prompt)
  2185.     free (current_readline_prompt);
  2186.       
  2187.       current_readline_prompt = temp_prompt;
  2188.     }
  2189.   else
  2190. #endif    /* READLINE */
  2191.     {
  2192.       if (interactive)
  2193.     {
  2194.       fprintf (stderr, "%s", temp_prompt);
  2195.       fflush (stderr);
  2196.     }
  2197.       free (temp_prompt);
  2198.     }
  2199. }
  2200.  
  2201. #include "maxpath.h"
  2202.  
  2203. /* Return a string which will be printed as a prompt.  The string
  2204.    may contain special characters which are decoded as follows:
  2205.    
  2206.     \t    the time
  2207.     \d    the date
  2208.     \n    CRLF
  2209.     \s    the name of the shell
  2210.     \w    the current working directory
  2211.     \W    the last element of PWD
  2212.     \u    your username
  2213.     \h    the hostname
  2214.     \#    the command number of this command
  2215.     \!    the history number of this command
  2216.     \$    a $ or a # if you are root
  2217.     \<octal> character code in octal
  2218.     \\    a backslash
  2219. */
  2220. #include <sys/param.h>
  2221. #include <time.h>
  2222.  
  2223. #define PROMPT_GROWTH 50
  2224. char *
  2225. decode_prompt_string (string)
  2226.      char *string;
  2227. {
  2228.   int result_size = PROMPT_GROWTH;
  2229.   int result_index = 0;
  2230.   char *result = (char *)xmalloc (PROMPT_GROWTH);
  2231.   int c;
  2232.   char *temp = (char *)NULL;
  2233.  
  2234.   result[0] = 0;
  2235.   while (c = *string++)
  2236.     {
  2237.       if (c == '\\')
  2238.     {
  2239.       c = *string;
  2240.  
  2241.       switch (c)
  2242.         {
  2243.         case '0':
  2244.         case '1':
  2245.         case '2':
  2246.         case '3':
  2247.         case '4':
  2248.         case '5':
  2249.         case '6':
  2250.         case '7':
  2251.           {
  2252.         char octal_string[4];
  2253.         int n;
  2254.  
  2255.         strncpy (octal_string, string, 3);
  2256.         octal_string[3] = '\0';
  2257.  
  2258.         n = read_octal (octal_string);
  2259.  
  2260.         temp = savestring ("\\");
  2261.         if (n != -1)
  2262.           {
  2263.             string += 3;
  2264.             temp[0] = n;
  2265.           }
  2266.  
  2267.         c = 0;
  2268.         goto add_string;
  2269.           }
  2270.       
  2271.         case 't':
  2272.         case 'd':
  2273.           /* Make the current time/date into a string. */
  2274.           {
  2275.         long the_time = time (0);
  2276.         char *ttemp = ctime (&the_time);
  2277.         temp = savestring (ttemp);
  2278.  
  2279.         if (c == 't')
  2280.           {
  2281.             strcpy (temp, temp + 11);
  2282.             temp[8] = '\0';
  2283.           }
  2284.         else
  2285.           temp[10] = '\0';
  2286.  
  2287.         goto add_string;
  2288.           }
  2289.  
  2290.         case 'n':
  2291.           temp = savestring ("\r\n");
  2292.           goto add_string;
  2293.  
  2294.         case 's':
  2295.           {
  2296.         extern char *shell_name;
  2297.         temp = savestring (shell_name);
  2298.         goto add_string;
  2299.           }
  2300.     
  2301.         case 'w':
  2302.         case 'W':
  2303.           {
  2304.         /* Use the value of PWD because it is much more effecient. */
  2305. #define EFFICIENT
  2306. #ifdef EFFICIENT
  2307.         char *polite_directory_format (), t_string[MAXPATHLEN];
  2308.  
  2309.         temp = get_string_value ("PWD");
  2310.  
  2311.         if (!temp)
  2312.           getwd (t_string);
  2313.         else
  2314.           strcpy (t_string, temp);
  2315. #else
  2316.         getwd (t_string);
  2317. #endif    /* EFFICIENT */
  2318.  
  2319.         if (c == 'W')
  2320.           {
  2321.             char *dir = (char *)rindex (t_string, '/');
  2322.             if (dir && dir != t_string)
  2323.               strcpy (t_string, dir + 1);
  2324.             temp = savestring (t_string);
  2325.           }
  2326.         else
  2327.           temp = savestring (polite_directory_format (t_string));
  2328.         goto add_string;
  2329.           }
  2330.       
  2331.         case 'u':
  2332.           {
  2333.         extern char *current_user_name;
  2334.         temp = savestring (current_user_name);
  2335.  
  2336.         goto add_string;
  2337.           }
  2338.  
  2339.         case 'h':
  2340.           {
  2341.         extern char *current_host_name;
  2342.         char *t_string;
  2343.  
  2344.         temp = savestring (current_host_name);
  2345.         if (t_string = (char *)index (temp, '.'))
  2346.           *t_string = '\0';
  2347.         
  2348.         goto add_string;
  2349.           }
  2350.  
  2351.         case '#':
  2352.           {
  2353.         extern int current_command_number;
  2354.         char number_buffer[20];
  2355.         sprintf (number_buffer, "%d", current_command_number);
  2356.         temp = savestring (number_buffer);
  2357.         goto add_string;
  2358.           }
  2359.  
  2360.         case '!':
  2361.           {
  2362.         extern int history_base, where_history ();
  2363.         char number_buffer[20];
  2364.  
  2365.         using_history ();
  2366.         if (get_string_value ("HISTSIZE"))
  2367.           sprintf (number_buffer, "%d",
  2368.                history_base + where_history ());
  2369.         else
  2370.           strcpy (number_buffer, "!");
  2371.         temp = savestring (number_buffer);
  2372.         goto add_string;
  2373.           }
  2374.  
  2375.         case '$':
  2376.           temp = savestring (geteuid () == 0 ? "#" : "$");
  2377.           goto add_string;
  2378.  
  2379.         case '\\':
  2380.           temp = savestring ("\\");
  2381.           goto add_string;
  2382.  
  2383.         default:
  2384.           temp = savestring ("\\ ");
  2385.           temp[1] = c;
  2386.  
  2387.         add_string:
  2388.           if (c)
  2389.         string++;
  2390.           result =
  2391.         (char *)sub_append_string (temp, result,
  2392.                        &result_index, &result_size);
  2393.           temp = (char *)NULL; /* Free ()'ed in sub_append_string (). */
  2394.           result[result_index] = '\0';
  2395.           break;
  2396.         }
  2397.     }
  2398.       else
  2399.     {
  2400.       while (3 + result_index > result_size)
  2401.         result = (char *)xrealloc (result, result_size += PROMPT_GROWTH);
  2402.  
  2403.       result[result_index++] = c;
  2404.       result[result_index] = '\0';
  2405.     }
  2406.     }
  2407.  
  2408.   /* I don't really think that this is a good idea.  Do you? */
  2409.   if (!find_variable ("NO_PROMPT_VARS"))
  2410.     {
  2411.       WORD_LIST *expand_string (), *list;
  2412.       char *string_list ();
  2413.  
  2414.       list = expand_string (result, 1);
  2415.       free (result);
  2416.       result = string_list (list);
  2417.       dispose_words (list);
  2418.     }
  2419.  
  2420.   return (result);
  2421. }
  2422.  
  2423. /* Report a syntax error, and restart the parser.  Call here for fatal
  2424.    errors. */
  2425. yyerror ()
  2426. {
  2427.   report_syntax_error ((char *)NULL);
  2428.   reset_parser ();
  2429. }
  2430.  
  2431. /* Report a syntax error with line numbers, etc.
  2432.    Call here for recoverable errors.  If you have a message to print,
  2433.    then place it in MESSAGE, otherwise pass NULL and this will figure
  2434.    out an appropriate message for you. */
  2435. report_syntax_error (message)
  2436.      char *message;
  2437. {
  2438.   if (message)
  2439.     {
  2440.       if (!interactive)
  2441.     {
  2442.       char *name = stream_name ? stream_name : "stdin";
  2443.       report_error ("%s:%d: `%s'", name, line_number, message);
  2444.     }
  2445.       else
  2446.     report_error ("%s", message);
  2447.  
  2448.       return;
  2449.     }
  2450.  
  2451.   if (shell_input_line && *shell_input_line)
  2452.     {
  2453.       char *error_token, *t = shell_input_line;
  2454.       register int i = shell_input_line_index;
  2455.       int token_end = 0;
  2456.  
  2457.       if (!t[i] && i)
  2458.     i--;
  2459.  
  2460.       while (i && (t[i] == ' ' || t[i] == '\t' || t[i] == '\n'))
  2461.     i--;
  2462.  
  2463.       if (i)
  2464.     token_end = i + 1;
  2465.  
  2466.       while (i && !member (t[i], " \n\t;|&"))
  2467.     i--;
  2468.  
  2469.       while (i != token_end && member (t[i], " \n\t"))
  2470.     i++;
  2471.  
  2472.       if (token_end)
  2473.     {
  2474.       error_token = (char *)alloca (1 + (token_end - i));
  2475.       strncpy (error_token, t + i, token_end - i);
  2476.       error_token[token_end - i] = '\0';
  2477.  
  2478.       report_error ("syntax error near `%s'", error_token);
  2479.     }
  2480.       else if ((i == 0) && (token_end == 0))    /* a 1-character token */
  2481.     {
  2482.       error_token = (char *) alloca (2);
  2483.       strncpy(error_token, t + i, 1);
  2484.       error_token[1] = '\0';
  2485.  
  2486.       report_error ("syntax error near `%s'", error_token);
  2487.     }
  2488.  
  2489.       if (!interactive)
  2490.     {
  2491.       char *temp = savestring (shell_input_line);
  2492.       char *name = stream_name ? stream_name : "stdin";
  2493.       int l = strlen (temp);
  2494.  
  2495.       while (l && temp[l - 1] == '\n')
  2496.         temp[--l] = '\0';
  2497.  
  2498.       report_error ("%s:%d: `%s'", name, line_number, temp);
  2499.       free (temp);
  2500.     }
  2501.     }
  2502.   else
  2503.     report_error ("Syntax error");
  2504. }
  2505.  
  2506. /* ??? Needed function. ??? We have to be able to discard the constructs
  2507.    created during parsing.  In the case of error, we want to return
  2508.    allocated objects to the memory pool.  In the case of no error, we want
  2509.    to throw away the information about where the allocated objects live.
  2510.    (dispose_command () will actually free the command. */
  2511. discard_parser_constructs (error_p)
  2512.      int error_p;
  2513. {
  2514. /*   if (error_p) {
  2515.      fprintf (stderr, "*");
  2516.   } */
  2517. }
  2518.    
  2519. /* Do that silly `type "bye" to exit' stuff.  You know, "ignoreeof". */
  2520.  
  2521. /* The number of times that we have encountered an EOF character without
  2522.    another character intervening.  When this gets above the limit, the
  2523.    shell terminates. */
  2524. int eof_encountered = 0;
  2525.  
  2526. /* The limit for eof_encountered. */
  2527. int eof_encountered_limit = 10;
  2528.  
  2529. /* If we have EOF as the only input unit, this user wants to leave
  2530.    the shell.  If the shell is not interactive, then just leave.
  2531.    Otherwise, if ignoreeof is set, and we haven't done this the
  2532.    required number of times in a row, print a message. */
  2533. handle_eof_input_unit ()
  2534. {
  2535.   extern int login_shell, EOF_Reached;
  2536.  
  2537.   if (interactive)
  2538.     {
  2539.       /* If the user wants to "ignore" eof, then let her do so, kind of. */
  2540.       if (find_variable ("ignoreeof") || find_variable ("IGNOREEOF"))
  2541.     {
  2542.       if (eof_encountered < eof_encountered_limit)
  2543.         {
  2544.           fprintf (stderr, "Use \"%s\" to leave the shell.\n",
  2545.                login_shell ? "logout" : "exit");
  2546.           eof_encountered++;
  2547.           /* Reset the prompt string to be $PS1. */
  2548.           prompt_string_pointer = (char **)NULL;
  2549.           prompt_again ();
  2550.           last_read_token = current_token = '\n';
  2551.           return;
  2552.         } 
  2553.     }
  2554.  
  2555.       /* In this case EOF should exit the shell.  Do it now. */
  2556.       reset_parser ();
  2557.       exit_builtin ((WORD_LIST *)NULL);
  2558.     }
  2559.   else
  2560.     {
  2561.       /* We don't write history files, etc., for non-interactive shells. */
  2562.       EOF_Reached = 1;
  2563.     }
  2564. }
  2565.